Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Diagnostics
Imports System.Threading
Imports System.IO
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Authentication
Imports System.Net.Security



Public Class TCPServerListener  ' The SOCKET LISTENER SERVER
    Private _syncRoot As New Object()
    Private _listening As Boolean
    Private listener As System.Net.Sockets.TcpListener
    Private TCPClientSocket As TCPClientSocketClient
    Private Shared TCPClients As New Hashtable
    Private EnableSSL As Boolean
    Private TimerDelay As Integer = 500 'Half a second delay forward to tcp client for packet purge
    Private serverCertificate As X509Certificate = Nothing
    Private stringRequiredToSendPackets As String = String.Empty
    Private ExcludeRequiredPacketsFromTransmission As Boolean = False

    Public Sub mainStopListen()
        If _listening Then
            SyncLock _syncRoot
                ' set listening flag
                _listening = False
                ' shut it down
                listener.[Stop]()
            End SyncLock
        End If
    End Sub
    Public Sub mainListen(IPAddress As String, _port As Integer, SSL As Boolean, CertificateFileName As String, _TimerDelay As Integer, _ReqdString As String, _ExcludeReqdString As Boolean, _MaximumThreads As Integer)
        Dim _address As System.Net.IPAddress
        Try
            SyncLock _syncRoot
                TimerDelay = _TimerDelay
                ExcludeRequiredPacketsFromTransmission = _ExcludeReqdString
                stringRequiredToSendPackets = _ReqdString
                If SSL And (File.Exists(CertificateFileName)) Then
                    EnableSSL = SSL
                    Try
                        serverCertificate = X509Certificate.CreateFromCertFile(CertificateFileName)
                    Catch ex As Exception
                        EnableSSL = False
                    End Try
                End If
                _address = System.Net.IPAddress.Parse(IPAddress)
                ThreadPool.SetMaxThreads(_MaximumThreads, _MaximumThreads)
                listener = New TcpListener(_address, _port)

                ' start the server
                listener.Start()

                ' set listening flag
                _listening = True
            End SyncLock

            ' Enter the listening loop
            Do
                ' Wait for connection
                Dim newClient As TcpClient = listener.AcceptTcpClient()
                ' queue a request to take care of the client
                ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ProcessClient), newClient)
            Loop While _listening
        Catch se As SocketException
            Trace.WriteLine("SocketException: " & se.ToString())
        Finally
            ' shut it down
            mainStopListen()
        End Try
    End Sub
    Private Sub ProcessClient(client As Object)
        Try

            TCPClientSocket = New TCPClientSocketClient(DirectCast(client, TcpClient), EnableSSL, serverCertificate, TimerDelay, stringRequiredToSendPackets, ExcludeRequiredPacketsFromTransmission)
            TCPClients.Add(TCPClients.Count + 1, TCPClientSocket)
        Catch ex As Exception
            'Swallow exception
        End Try
    End Sub
End Class



